﻿
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}

<link href="~/ui/plugins/jquery-layout/jquery.layout-latest.css" rel="stylesheet" />
<link href="~/ui/plugins/jquery-ztree/3.5/css/metro/zTreeStyle.css" rel="stylesheet" />
<div class="ui-layout-west">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-grid"></i>栏目列表
            </div>
        </div>
        <div class="ui-layout-content">
            <div id="tplTree" class="ztree"></div>
        </div>
    </div>
</div>
<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="searchForm">
                    <div class="select-list">
                        <ul>
                            <li>
                                模板名称：<input type="text" name="templateName" id="templateName" />
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" id="searchButton"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" id="resetButton"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="add();" data-id="0" data-role="cms:template:add">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-primary single disabled" onclick="edit(this);" data-id="0" data-role="cms:template:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger single disabled" onclick="removeAll(this);" data-id="0" data-role="cms:template:delete">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-info single disabled" onclick="copyTemplate(this)" data-id="0" data-role="cms:template:copy">
                    <i class="fa fa-clone"></i>模板复制
                </a>
                <a class="btn btn-warning single disabled" onclick="syncLocalFile(this)" data-id="0" data-role="cms:template:sync">
                    <i class="fa fa-refresh"></i> 同步本地文件
                </a>
            </div>

            <div class="col-sm-12 select-table table-striped">
                <table id="dataTable"></table>
            </div>
        </div>
    </div>
</div>
@section scripts{
    <script src="~/ui/plugins/jquery-layout/jquery.layout-latest.js"></script>
    <script src="~/ui/plugins/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js"></script>
    <script>
        var $table, pid, lastChannelNode;
        $(function () {
            let panehHidden = false;
            if ($(this).width() < 769) {
                panehHidden = true;
            }
            $('body').layout({ initClosed: panehHidden, west__size: 200 });
            // 回到顶部绑定
            if ($.fn.toTop !== undefined) {
                var opt = {
                    win: $('.ui-layout-center'),
                    doc: $('.ui-layout-center')
                };
                $('#scroll-up').toTop(opt);
            }

            jutils.handleMessage(function (res) {
                //console.log(res);
                if (res.title === 'cms_template_form') {
                    $table.easyTable('search');
                }

            });
            loadChannelTree();
            loadGrid();
        });


        function loadChannelTree() {
            var opts = {

                data: {
                    simpleData: {
                        enable: true,
                        pIdKey: "pId",
                    }
                }, callback: {
                    onClick: DicZtreeClick

                }, check: {
                    enable: true,
                    chkStyle: "radio"
                }
            };
            jutils.ajaxGet('/Cms/TemplateCode/TemplateCategory', null, function (res) {

                var templates = res.data.templates;
                var defaultModel = { id: 0, name: "全部", pId: 0, open: true, title: '' };
                templates.splice(0, 0, defaultModel);
                tplTree = $.fn.zTree.init($("#tplTree"), opts, res.data.templates);
                tplTree.expandAll(true);
            });
        }

        //单击事件
        function DicZtreeClick(event, treeId, treeNode) {
            if (lastChannelNode) {
                tplTree.checkNode(lastChannelNode, false, true);
            }
            tplTree.checkNode(treeNode, true, true);
            lastChannelNode = treeNode;
            pid = treeNode.id;
            $table.easyTable('search');
        }


        function loadGrid() {
            let options={
                url: '/cms/templatecode/GetData',
                pageNumber: 1,                      // 初始化加载第一页，默认第一页,并记录
                pageSize: 10,                       // 每页的记录行数（*）
                pageList: "10, 25, 50, 100",        // 可供选择的每页的行数（*）
                uniqueId: 'id',
                queryParams: function (params) {
                    let p = {
                        pageNumber: params.pageNumber,
                        pageSize: params.pageSize,
                        templateName: $('#templateName').val(),
                        pid: pid
                    }
                    return p;
                },
                columns: [
                    { field: 'selectItem', radio: true },
                    { field: 'template_name', title: '模板名称', align: 'left' },
                    { field: 'template_file', title: '模板文件', align: 'center' },
                    { field: 'file_name', title: '生成文件名', align: 'center' },
                    {
                        field: 'template_mode', title: '类型', align: 'center', formatter: function (v) {
                            if (v === 1) {
                                return '首页模板';
                            } else if (v === 2) {
                                return '栏目模板';
                            } else if (v === 3) {
                                return '内容模板';
                            } else if (v === 4) {
                                return '单页模板';
                            } else if (v === 5) {
                                return '包含文件';
                            }
                            return '';
                        }
                    },
                    {
                        field: 'is_default', title: '默认模板', align: 'center', formatter: function (v) {
                            if (v === 1) {
                                return '<span class="label label-primary">默认模板</span>';
                            }
                            return '';
                        }
                    },
                    {
                        field: 'id', title: '操作', align: 'center',
                        formatter: render
                    }
                ]

            };

            $table = $('#dataTable').easyTable(options);
        }

        function render(v, r, i) {
            if (r.template_mode === 1) {
                var genHtml = '<a class="btn btn-info btn-xs" title="生成首页" href="javascript:void(0);" onclick="genHome()" data-id="' + v + '"><i class="fa fa-rocket"></i>生成首页</a>';
                return genHtml;
            }

            return '';
        }

        function genHome(obj) {

            jutils.ajaxGet('/content/GenerateHome', null, function (res) {
                if (res.status) {
                    jutils.success("首页生成成功");
                }
            });
        }
        function renderTitle(v, d) {
            return '<a href="/cms/content/' + d.channel_id + '/' + d.id + '.html" target="_blank">' + v + '</a>';
        }
        function add() {

            var url = '/cms/templatecode/Form';
            jutils.openTab('cms_template_form', '添加模板', url, { id:0 });
        }



        function edit(obj) {

            let rows = $table.easyTable("selected");
            if (rows.length == 0) {
                jutils.warn('请选择数据行');
                return;
            }


            var url = '/cms/templatecode/Form';
            jutils.openTab('cms_template_form', '修改模板', url, { id: rows[0].id });
           
        }

        function removeAll(obj) {
            let rows = $table.easyTable("selected");
            if (rows.length == 0) {
                jutils.warn('请选择数据行');
                return;
            }
            jutils.confirm("确认删除当前模板吗", function () {
                jutils.ajaxGet('/cms/templatecode/DeleteById', { id: rows[0].id }, function (res) {
                    $table.easyTable('search');
                })
            })
        }



        function copyFile(obj) {

            let rows = $table.easyTable("selected");
            if (rows.length == 0) {
                jutils.warn('请选择数据行');
                return;
            }
         
            jutils.confirm("确认复制一份新的模板副本吗", function () {
                jutils.ajaxGet('/cms/templatecode/copyFile', { id: rows[0].id }, function (res) {
                    $table.easyTable('refresh');
                })
            })
        }

        function syncLocalFile(obj) {
            let rows = $table.easyTable("selected");
            if (rows.length == 0) {
                jutils.warn('请选择数据行');
                return;
            }

            jutils.confirm("确认本地模板同步到数据库吗", function () {
                jutils.ajaxGet('/cms/templatecode/syncTemplate', { id: rows[0].id }, function (res) {
                    $table.easyTable('search');
                })
            })
        }
    </script>

}
